package com.jcl.recruit.login.service.impl;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import com.jcl.recruit.login.service.UserExternalAccessLogin;
import com.jcl.recruit.login.vo.UserExternalLoginInfo;
import com.jcl.recruit.user.logic.UserExternalAccessLogic;
import com.jcl.recruit.user.model.UserExternalAccess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName UserExternalAccessLoginImpl
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/19 9:25
 * @Version 1.0
 **/
@Component("userExternalAccessLogin")
public class UserExternalAccessLoginImpl implements UserExternalAccessLogin {

    @Autowired
    protected UserExternalAccessLogic userExternalAccessLogic;

    @Override
    public UserExternalLoginInfo loginByUsername(String username, String password) {

        // username 平台唯一 
        List<UserExternalAccess> accessList = userExternalAccessLogic.findListByUsername(
                                      username, null, null);
        if(CollectionUtils.isEmpty(accessList)){
            UserExternalAccess userExternalAccess = accessList.get(0);
            UserExternalLoginInfo userExternalLoginInfo = makeUserExternalLoginInfo(userExternalAccess);
            
            return userExternalLoginInfo;
        }

        return null;
    }

    @Override
    public UserExternalLoginInfo loginByExternalKey(String externalKey, String category) {

        // 查询正在使用的 账号
        List<UserExternalAccess> accessList = userExternalAccessLogic.findListByExternalKey(
                externalKey, category, UserExternalLoginConstant.USER_ACCESS_STATUS_USED);
        if(CollectionUtils.isNotEmpty(accessList)){
            UserExternalAccess userExternalAccess = accessList.get(0);
            UserExternalLoginInfo userExternalLoginInfo = makeUserExternalLoginInfo(userExternalAccess);

            return userExternalLoginInfo;

        }
        return null;
    }
    
    /* *
     * @Author yyliu
     * @Description //TODO 
     * @Date 9:43 2018/10/19
     * @Param [externalAccess]
     * @return com.example.logic.login.UserExternalLoginInfo
     **/
    private UserExternalLoginInfo makeUserExternalLoginInfo(UserExternalAccess externalAccess){
        UserExternalLoginInfo info = new UserExternalLoginInfo();
        info.setUsername(externalAccess.getUsername());
        info.setPassword(externalAccess.getPassword());
        info.setUserId(externalAccess.getUserId());
        info.setCorpCode(externalAccess.getCorpCode());
        return info;
    }
}
